﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xf.Entitys;
using xf.Service;

namespace xf
{
    public partial class FSaveCustomer : Form
    {
        public string CusId = "";
        public CUS_NAME CusNameObj = null; 

        public FSaveCustomer(string cusId = "")
        {
            InitializeComponent();
            GetCustomer(cusId); // 必须初始化组件后才能使用
        }

        private void GetCustomer(string cusId = "")
        {
            CusId = cusId;
            if (string.IsNullOrEmpty(CusId)) {
                return;
            }
            Dictionary<string, string> searchParam = new Dictionary<string, string> {
                { "Cus_id", CusId},
            };
            List<CUS_NAME> cusNames = CustomerService.GetInstance().GetCustomer(searchParam, false);
            if (cusNames.Count > 0)
            {
                CusNameObj = cusNames.First<CUS_NAME>();
            }
            else
            {
                MessageBox.Show("修改页面获取客户信息失败");
            }
        }

        /// <summary>
        /// 关闭新增、修改客户信息窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelAddCustomer_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        /// <summary>
        /// 触发添加/更新客户操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [Obsolete]
        private void triggerAddCustomer_Click(object sender, EventArgs e)
        {
            if (CusNameObj == null)
            { // 添加客户
                Dictionary<string, string> param = new Dictionary<string, string> {
                    {"Cus_id", cusId.Text},
                    {"Cus_name", cusName.Text}
                };
                Dictionary<string, string> detail = new Dictionary<string, string> { };
                if (CustomerService.GetInstance().AddCustomer(param, ref detail))
                {
                    MessageBox.Show("添加成功");
                    this.Close();
                    this.Dispose();
                    FCustomerList.StaticReloadCustomerList();
                }
                else
                {
                    detail.TryGetValue("msg", out string msg);
                    MessageBox.Show("添加失败：" + msg);
                }
            }
            else 
            { // 更新客户
                Dictionary<string, string> param = new Dictionary<string, string> {
                    {"Cus_id", cusId.Text},
                    {"Cus_name", cusName.Text}
                };
                Dictionary<string, string> detail = new Dictionary<string, string> { };
                if (CustomerService.GetInstance().UpdateCustomer(param, ref detail))
                {
                    MessageBox.Show("更新成功");
                    this.Close();
                    this.Dispose();
                    FCustomerList.StaticReloadCustomerList();
                }
                else
                {
                    detail.TryGetValue("msg", out string msg);
                    MessageBox.Show("更新失败：" + msg);
                }
            }
            
        }

        /// <summary>
        /// 当加载的时候，有对应客户信息的时候，要在前端界面显示出来
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddCustomerEnter(object sender, EventArgs e)
        {
            if (CusNameObj != null) { 
                cusId.Text = CusNameObj.Cus_id.ToString().Trim();
                cusName.Text = CusNameObj.Cus_name.ToString().Trim();
            }
        }
    }
}
